/*
** my_printf.c for  in /home/gueux_g/projet/my_printf/bin
**
** Made by gueux-g
** Login   <gueux_g@epitech.net>
**
** Started on  Mon Nov  5 17:50:07 2012 gueux-g
** Last update Tue Dec 18 01:34:47 2012 gregory gueux
*/

#include	<stdio.h>
#include	<stdarg.h>
#include	"hearder.h"

void		my_printf_init(void (*ptrfonc[11])(va_list))
{
  ptrfonc[0] = my_percent;
  ptrfonc[1] = my_nbr;
  ptrfonc[2] = my_nbr;
  ptrfonc[3] = my_binarie;
  ptrfonc[4] = my_octal;
  ptrfonc[5] = my_deca;
  ptrfonc[6] = my_mini_hexa;
  ptrfonc[7] = my_maxi_hexa;
  ptrfonc[8] = my_char;
  ptrfonc[9] = my_str;
  ptrfonc[10] = my_sp_str;
}

void		my_string(void (*ptrfonc[11])(va_list), char *str,
			  int i, va_list ap)
{
  int		count;
  char		*string;

  string = "%dibouxXcsS";
  count = 0;
  while (string[count] != str[i + 1])
    count++;
  if (count > 11)
    {
      my_putchar('%');
      my_putchar(str[i + 1]);
    }
  else
    ptrfonc[count](ap);
  count = 0;
}

int		my_printf(char *str, ...)
{
  int		i;
  va_list	ap;
  void      	(*ptrfonc[11])(va_list );

  va_start(ap, str);
  my_printf_init(ptrfonc);
  i = 0;
  while (str[i])
    {
      if (str[i] == '%')
	{
	  my_string(ptrfonc, str, i, ap);
	  i++;
	}
      else
	my_putchar(str[i]);
      i++;
    }
  va_end(ap);
  return (i);
}
